package com.hknc.btscale.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.gson.Gson;
import com.hknc.btscale.bean.ChangeUser;
import com.hknc.btscale.bean.Constants;
import com.hknc.btscale.bean.GoodsBean;
import com.hknc.btscale.bean.LoginResponse;
import com.hknc.btscale.bean.PlayBean;
import com.hknc.btscale.bean.SysConfingDataBean;
import com.hknc.btscale.bean.SysDataLook;
import com.hknc.btscale.bean.TicketBean;
import com.hknc.btscale.bean.TodayCheapBean;
import com.hknc.btscale.bean.VersionApkBean;
import com.hknc.btscale.bean.WSMessage;
import com.hknc.btscale.utils.BTSendDataUtils;

/**
 * 本广播用来获取ws下发推送的数据，
 * 并且根据接收到指令的不同，再下发给蓝牙称
 */
public class WSDataReceiver extends BroadcastReceiver {
    private Gson mGson = new Gson();
    private static final String TAG = "WSDataReceiver";
    private WSMessage wsMessage;
    private DoSomethingCallBack mDoSomethingCallBack;

    public void setDoSomethingCallBack(DoSomethingCallBack doSomethingCallBack) {
        mDoSomethingCallBack = doSomethingCallBack;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String message = intent.getStringExtra("message");

        if (Constants.WS_CONNECT_SUCCESS.equals(message)) {
            mDoSomethingCallBack.success();
            return;
        }
        wsMessage = mGson.fromJson(message, WSMessage.class);
        switch (wsMessage.getMsg()) {
            //播放规则
            case Constants.PLAY_RULES:
                String str = "{\"data\":" + wsMessage.getData() + "}";
                PlayBean playBean = mGson.fromJson(str, PlayBean.class);
                mDoSomethingCallBack.setPlayRules(playBean);
                break;
            //开称锁称
            case Constants.TURN_ON_OR_TURN_OFF://开-锁称
                if (wsMessage.getData().equals("0")) {
                    BTSendDataUtils.sendData(intent.getParcelableExtra("device"), 0xA0);
                } else {
                    BTSendDataUtils.sendData(intent.getParcelableExtra("device"), 0xA1);
                }
                break;
            case Constants.NUMBER_OF_TRANSACTION://交易笔数
                mDoSomethingCallBack.setTransactionNum(wsMessage.getData());
                break;
            case Constants.SMALL_TICKET_INFO://小票
                String strTicket = wsMessage.getData();
                TicketBean ticketBean = mGson.fromJson(strTicket, TicketBean.class);
                mDoSomethingCallBack.setTicketInfo(ticketBean);
                break;
            case Constants.TODAY_CHEAP://今日特价
                String strTodayCheap = "{\"data\":" + wsMessage.getData() + "}";
                TodayCheapBean todayCheapBean = mGson.fromJson(strTodayCheap, TodayCheapBean.class);
                mDoSomethingCallBack.setTodayCheap(todayCheapBean);
                break;
            case Constants.PLU_INFO://PLU信息
                String strPlu = "{\"data\":" + wsMessage.getData() + "}";
//                PluBean pluBean = mGson.fromJson(strPlu, PluBean.class);
                GoodsBean goodsBean = mGson.fromJson(strPlu, GoodsBean.class);
                mDoSomethingCallBack.setPluInfo(goodsBean);
                break;
            case Constants.RATE_OF_CHECK://检测合格率
                String strRateOfCheck = wsMessage.getData();
                mDoSomethingCallBack.setRateOfCheck(strRateOfCheck);
                break;
            case Constants.CLEAR_PLU://清空plu
                String strClearPlu = wsMessage.getData();
                mDoSomethingCallBack.setClearPlu(strClearPlu);
                break;
            case Constants.GOODS_LIST://商品展示推送
                String strGoodsBean = "{\"data\":" + wsMessage.getData() + "}";
                Log.e(TAG, "onReceive: " + strGoodsBean);
                GoodsBean goodsBeanAll = mGson.fromJson(strGoodsBean, GoodsBean.class);
                mDoSomethingCallBack.setGoodsBean(goodsBeanAll);
                break;
            case Constants.USER_INFO:
                String strUserInfo = "{\"code\":200,\"message\":\"推送商户信息\",\"data\":" + wsMessage.getData() + "}";
                LoginResponse loginResponse = mGson.fromJson(strUserInfo, LoginResponse.class);
                mDoSomethingCallBack.setUserInfo(loginResponse);
                break;
            case Constants.MARKET_CHECK:
                String strMarketCheck = wsMessage.getData();
                mDoSomethingCallBack.setMarketCheck(strMarketCheck);
                break;
            case Constants.CHECK_TIME:
                BTSendDataUtils.sendData(intent.getParcelableExtra("device"), 0xC4);
                mDoSomethingCallBack.setCheckTime();
                break;
            case Constants.UPDATE_VERSION:
                String strUpdateVersion = "{\"code\":200,\"message\":\"推送商户信息\",\"data\":" + wsMessage.getData() + "}";
                VersionApkBean versionApkBean = mGson.fromJson(strUpdateVersion, VersionApkBean.class);
                mDoSomethingCallBack.updateVersion(versionApkBean);
                break;
            case Constants.SYSTEM_DATA_CONFIG:
                SysConfingDataBean sysConfingDataBean = mGson.fromJson(wsMessage.getData(), SysConfingDataBean.class);
                mDoSomethingCallBack.setSysConfig(sysConfingDataBean.getIndexNo(), sysConfingDataBean.getValue());
                break;
            case Constants.SYSTEM_DATA_LOOK:
                SysDataLook sysDataLook = mGson.fromJson(wsMessage.getData(), SysDataLook.class);
                mDoSomethingCallBack.getSysConfig(sysDataLook.getIndexNo());
                break;
            case Constants.CHANGE_USER:
                String changeStr=wsMessage.getData();
                ChangeUser changeUser=mGson.fromJson(changeStr,ChangeUser.class);
                mDoSomethingCallBack.changeUser(changeUser);
                break;
        }
    }

    public interface DoSomethingCallBack {
        void success();

        void setType(int type);

        void setPlayRules(PlayBean playBean);

        void setTodayCheap(TodayCheapBean bean);

        void setPluInfo(GoodsBean goodsBean);

        void setTicketInfo(TicketBean ticketBean);

        void setRateOfCheck(String str);

        void setClearPlu(String str);

        void setGoodsBean(GoodsBean goodsBean);

        void setTransactionNum(String numOfTranaction);

        void setUserInfo(LoginResponse loginResponse);

        void setMarketCheck(String str);

        void setCheckTime();

        void updateVersion(VersionApkBean versionApkBean);

        void setSysConfig(int sysNum, int sysValue);

        void getSysConfig(int indexNo);

        void changeUser(ChangeUser changeUser);
    }

}